import type { RouteRecordRaw } from "vue-router";

export function mapMenusToRoutes(menus: any) {
  const routes: RouteRecordRaw[] = []
  // 读取views下所有文件
  const modules = import.meta.glob('@/views/**/*.vue')
  // 动态添加路由
  menus.forEach((item: { children: any[] }) => {
    item.children.forEach(subMenu => {
      if (modules[`/src/views${subMenu.path}.vue`]) {
        routes.push({
          path: subMenu.path,
          name: subMenu.compontent,
          component: modules[`/src/views${subMenu.path}.vue`],
          meta: subMenu.meta
        })
      }
    });
  });
  return routes
}

export function mapMenusToBreadcrumb(path: string, menus: any[]) {
  console.log(path, menus);
  const breadcrumb: any[] = [];
  for (const menu of menus) {
    for (const subMenu of menu.children) {
      if (path != "/layout/homePage" && subMenu.path === path) {
        breadcrumb.push({ id: menu.id, name: menu.name, path: menu.path });
        breadcrumb.push({ id: subMenu.id, name: subMenu.name, path: subMenu.path });
      }
    }
  }
  return breadcrumb;
}
